package com.my.hzb.common.util;



import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.my.hzb.common.bean.ResultBean;

public class HttpUtil {

	private static final Logger log = LogManager.getLogger(HttpUtil.class);
    private static final String CHARSET_GBK = "gbk";
    private static final String CHARSET_UTF = "utf-8";
    private static final int defaultTimeout = 30000;
    
    public static String requestPostDefau(String url,Map<String,String> keyValueMap)
	{
    	return requestPost(url,keyValueMap,"utf-8",50000);
	}
    
    public static String requestGostDefau(String url)
	{
    	return requestGost(url,"utf-8",50000);
	}
    public static String requestGost(String url,String charSet, int timeout)
	{
		HttpClient client = new HttpClient();
		GetMethod getMethod = new GetMethod(url);
		String var19 = null;
		try {
			int var16 = client.executeMethod(getMethod);
            if(var16 != 200) {
                log.info("request \'" + url + "\' failed,the status is not 200,status:" + var16);
                String var18 = "";
                return var18;
            }

            InputStream var17 = getMethod.getResponseBodyAsStream();
            var19 = getStringByStream(var17, charSet);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			getMethod.releaseConnection();
            client.getHttpConnectionManager().closeIdleConnections(0L);
        }
		log.info("url="+url+",返回的结果是="+var19);
		return var19;
	}
	public static String requestPost(String url,Map<String,String> keyValueMap,String charSet, int timeout)
	{
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod(url);
        if(keyValueMap != null) {
            NameValuePair[] e = new NameValuePair[keyValueMap.size()];
            Iterator inputStream = keyValueMap.entrySet().iterator();

            for(int c = 0; inputStream.hasNext(); ++c) {
                Entry entry = (Entry)inputStream.next();
                NameValuePair nvp = new NameValuePair();
                nvp.setName((String)entry.getKey());
                nvp.setValue((String)entry.getValue());
                e[c] = nvp;
            }

            postMethod.addParameters(e);
        }

        String var19;
        try {
            log.debug("query uri ===============" + postMethod.getURI());
            postMethod.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler());
            postMethod.getParams().setParameter("http.protocol.content-charset", charSet);
            postMethod.getParams().setParameter("http.socket.timeout", Integer.valueOf(timeout));
            int var16 = client.executeMethod(postMethod);
            if(var16 != 200) {
                log.info("request \'" + url + "\' failed,the status is not 200,status:" + var16);
                String var18 = "";
                return var18;
            }

            InputStream var17 = postMethod.getResponseBodyAsStream();
            var19 = getStringByStream(var17, charSet);
        } catch (Exception var14) {
            log.error("executeHttpRequestString发生异常！请检查网络和参数url:" + url, var14);
            return null;
        } finally {
            postMethod.releaseConnection();
            client.getHttpConnectionManager().closeIdleConnections(0L);
        }
        log.info("url="+url+",params="+keyValueMap+",返回的结果是="+var19);
        return var19;
	}
	private static String getStringByStream(InputStream inputStream, String charSet) throws IOException {
        String str = "";
        if(inputStream != null) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096);
            byte[] buffer = new byte[4096];

            int len;
            while((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }

            outputStream.close();
            byte[] responseBody = outputStream.toByteArray();
            if(responseBody != null) {
                str = new String(responseBody, 0, responseBody.length, charSet);
            }
        }

        return str;
    }
	
	public static ResultBean getResultByHttp(String url,Map<String,String> keyValueMap){
		String result = HttpUtil.requestPost(url, keyValueMap, "utf-8", 70000);
		System.out.println("result="+result);
		ResultBean resultBean = JsonUtil.g.fromJson(result, ResultBean.class);
		
		return resultBean;
	}
}
